package android.slc.module.component.attchment.entity;

import android.slc.box.attachment.bean.Progress;
import android.slc.box.attachment.loading.LoadingAttachment;
import android.slc.box.commonlibrary.compat.SlcResourceUtils;
import android.slc.box.mp.picker.entity.i.IBaseItem;
import android.slc.module.component.attchment.utils.AttachmentUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.Serializable;

public class Attachment extends LoadingAttachment<IBaseItem, AttFileInfo, FileInfoImp, Progress> implements Serializable {
    private int isAllowEditStatus = AttachmentUtils.IS_ALLOW_EDIT_STATUS_EDF;
    private String linkId;

    public Attachment(AttFileInfo netBody) {
        this.mNetBody = netBody;
    }

    public Attachment(String linkId, AttFileInfo netBody) {
        this.linkId = linkId;
        this.mNetBody = netBody;
    }

    public Attachment(IBaseItem iBaseItem) {
        this.mLocalBody = iBaseItem;
    }

    public Attachment(String linkId, IBaseItem iBaseItem) {
        this.linkId = linkId;
        this.mLocalBody = iBaseItem;
    }

    @Override
    public void setLocalPath(String path) {
        if (mLocalBody != null) {
            mLocalBody.setPath(path);
        }
    }

    @Override
    public String getLocalPath() {
        if (mLocalBody != null) {
            return mLocalBody.getPath();
        }
        return null;
    }

    @Override
    public void setNetPath(String path) {
        mNetBody.setNetBodyPath(path);
    }

    @Override
    public String getNetPath() {
        return mNetBody.getNetBodyPath();
    }

    public String getLinkId() {
        return linkId;
    }

    public void setLinkId(String linkId) {
        this.linkId = linkId;
    }

    /**
     * 获取附件名称
     *
     * @return
     */
    public String getAttachmentName() {
        if (isLocalBody()) {
            return getLocalBody().getDisplayName();
        }
        if (isNetBody() && !StringUtils.isEmpty(getNetBody().getNetBodyName())) {
            return getNetBody().getNetBodyName();
        }
        return SlcResourceUtils.getString("app_label_unknown_file");
    }

    public String getExpandName() {
        if (isLocalBody()) {
            return getLocalBody().getExtension();
        }
        if (isNetBody() && !StringUtils.isEmpty(getNetBody().getNetBodyName())) {
            int lastPoi = getNetBody().getNetBodyName().lastIndexOf('.');
            int lastSep = getNetBody().getNetBodyName().lastIndexOf(File.separator);
            if (lastPoi == -1 || lastSep >= lastPoi) return "";
            return getNetBody().getNetBodyName().substring(lastPoi + 1);
        }
        return "";
    }

    public void setIsAllowEditStatus(int isAllowEditStatus) {
        this.isAllowEditStatus = isAllowEditStatus;
    }

    public int getIsAllowEditStatus() {
        return isAllowEditStatus;
    }

    public boolean isAllowEdit() {
        switch (isAllowEditStatus) {
            case AttachmentUtils.IS_ALLOW_EDIT_STATUS_EDF:
                return isLocalBody();
            case AttachmentUtils.IS_ALLOW_EDIT_STATUS_AGREE:
                return true;
            case AttachmentUtils.IS_ALLOW_EDIT_STATUS_DISAGREE:
                return false;
            default:
                return isLocalBody();
        }
    }
}
